﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VolunteerHubAdapter.ViewModels
{
    public class SettingsViewModel : MVVM.ViewModels.WorkspaceViewModel
    {
        public Adapter Adapter { get; set; }

        private string _url;
        public string URL
        {
            get { return _url; }
            set
            {
                if (Object.Equals(_url, value) == false)
                {
                    _url = value;
                    this.OnPropertyChanged();
                    this.OnPropertyChanged("IsEnabled");
                }
            }
        }

        private string _user;
        public string User
        {
            get { return _user; }
            set
            {
                if (Object.Equals(_user, value) == false)
                {
                    _user = value;
                    this.OnPropertyChanged();
                    this.OnPropertyChanged("IsEnabled");
                }
            }
        }

        private string _password;
        public string Password
        {
            get { return _password; }
            set
            {
                if (Object.Equals(_password, value) == false)
                {
                    _password = value;
                    this.OnPropertyChanged();
                    this.OnPropertyChanged("IsEnabled");
                }
            }
        }

        private DateTime _lastRunDate;
        public DateTime LastRunDate
        {
            get { return _lastRunDate; }
            set
            {
                if (Object.Equals(_lastRunDate, value) == false)
                {
                    _lastRunDate = value;
                    this.OnPropertyChanged();
                    this.OnPropertyChanged("IsEnabled");
                }
            }
        }

        private DateTime _endRunDate;
        public DateTime EndRunDate
        {
            get { return _endRunDate; }
            set
            {
                if (Object.Equals(_endRunDate, value) == false)
                {
                    _endRunDate = value;
                    this.OnPropertyChanged();
                    this.OnPropertyChanged("IsEnabled");
                }
            }
        }

        private bool _showDates;
        public bool ShowDates
        {
            get { return _showDates; }
            set
            {
                if (!object.Equals(_showDates, value))
                {
                    _showDates = value;
                    this.OnPropertyChanged();
                }
            }
        }

        private Models.Settings _settings;
        public SettingsViewModel(Models.Settings settings)
        {
            if (settings == null)
                throw new ArgumentNullException("settings", "The settings model is null.");

            _settings = settings;
            this.URL = _settings.URL;
            this.User = _settings.User;
            this.Password = _settings.Password;
            this.LastRunDate = _settings.LastRunDate;

            this.EndRunDate = DateTime.Now.AddDays(1);

            this.CheckHttpsUrl();
        }

        private MVVM.Commands.RelayCommand _saveCommand;
        public MVVM.Commands.RelayCommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                    _saveCommand = new MVVM.Commands.RelayCommand((param) => this.Save());

                return _saveCommand;
            }
        }

        private MVVM.Commands.RelayCommand _testCommand;
        public MVVM.Commands.RelayCommand TestCommand
        {
            get
            {
                if (_testCommand == null)
                    _testCommand = new MVVM.Commands.RelayCommand((param) => this.TestConnection());

                return _testCommand;
            }
        }

        private Models.Settings GetCurrentSettingsModel()
        {
            this.CheckHttpsUrl();

            return new Models.Settings()
            {
                URL = this.URL,
                User = this.User,
                Password = this.Password,
                LastRunDate = this.LastRunDate,
                EndRunDate = this.EndRunDate
            };
        }

        public void TestConnection()
        {
            VolunteerHubEventArgs args = new VolunteerHubEventArgs();
            try
            {
                using (VolunteerHubHelper helper = new VolunteerHubHelper(GetCurrentSettingsModel()))
                {
                    args.Successful = (helper.TestConnection() == true);
                    if (args.Successful == false) args.ErrorMessage = "The connection was not available.";
                }
            }
            catch (Exception ex)
            {
                args.Successful = false;
                args.ErrorMessage = ex.Message;
            }

            if (this.TestConnectionResult != null) this.TestConnectionResult(this, args);
        }

        public delegate void TestConnectionResultHandler(object sender, VolunteerHubEventArgs e);
        public event TestConnectionResultHandler TestConnectionResult;

        public void Save()
        {
            this.CheckHttpsUrl();

            _settings.URL = this.URL;
            _settings.User = this.User;
            _settings.Password = this.Password;
            _settings.LastRunDate = this.LastRunDate;
            _settings.EndRunDate = this.EndRunDate;

            this.Close();
        }

        public Boolean IsEnabled
        {
            get
            {
                if ((this.URL == null) || (this.URL == string.Empty)) return false;
                if ((this.User == null) || (this.User == string.Empty)) return false;
                if ((this.Password == null) || (this.Password == string.Empty)) return false;

                return true;
            }
        }
        public string Version
        {
            get
            {
                return "v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        private MVVM.Commands.RelayCommand _helpCommand;
        public MVVM.Commands.RelayCommand HelpCommand
        {
            get
            {
                if (_helpCommand == null)
                {
                    _helpCommand = new MVVM.Commands.RelayCommand((param) => this.RaiseShowHelp());
                }
                return _helpCommand;
            }
        }
        private void RaiseShowHelp()
        {
            if (ShowHelp != null) ShowHelp(this, EventArgs.Empty);
        }

        public delegate void ShowHelpHandler(object sender, EventArgs e);
        public event ShowHelpHandler ShowHelp;

        private void CheckHttpsUrl()
        {
            if (this.URL.Contains("http:"))
            {
                this.URL = this.URL.Replace("http:", "https:");
            }
            else if (!this.URL.Contains("http://") && !this.URL.Contains("https://"))
            {
                this.URL = "https://" + this.URL;
            }
        }

    }
}
